package view.component.window
{
import core.Language;
import core.manager.CostManager;
import core.manager.RequirementManager;
import core.model.GameModel;

import framework.view.components.button.ButtonProxy;
import framework.view.components.container.RepeaterProxy;

import mapengine.interfaces.IMapBuilding;

import values.item.BuildingItemValue;
import values.item.ItemValue;

import view.component.asset.IconProxy;
import view.component.render.BuildingUpgradeRequirementItemRender;
import view.component.render.ResourceRequirementItemRender;

/**
 * UpgradeBuildingUIProxy
 * @author jliang
 *
 */
public class UpgradeBuildingUIProxy extends WindowProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function UpgradeBuildingUIProxy()
	{
		super();
	}
	//==========================================================================
	//  Variables
	//==========================================================================
	protected function get icon():IconProxy
	{
		return proxys["icon"];
	}
	
	protected function get requirementList():RepeaterProxy
	{
		return proxys["requirementList"];
	}
	
	protected function get costList():RepeaterProxy
	{
		return proxys["costList"];
	}
	
	protected function get upgradeWithCashBtn():ButtonProxy
	{
		return proxys["upgradeWithCashBtn"];
	}
	
	protected function get upgradeBtn():ButtonProxy
	{
		return proxys["upgradeBtn"];
	}
	
	protected var isReachRequirement:Boolean = false;
	//==========================================================================
	//  Properties
	//==========================================================================
	protected var _data:IMapBuilding;
	
	public function get data():IMapBuilding
	{
		return _data;
	}
	
	public function set data(value:IMapBuilding):void
	{
		_data = value;
		
		init();
	}
	//==========================================================================
	//  Overridden methods: WindowProxy
	//==========================================================================
	override protected function get proxyMapping():Array
	{
		return [ 
			{ skin:"", classRef: IconProxy, id:"icon" },
			{ skin:"upgradeBtn", classRef: ButtonProxy, id:"upgradeBtn", text:Language.getText("@upgrade_building_button_label"), onClick: onClickToUpradeHandler },
			{ skin:"upgradeWithCashBtn", classRef: ButtonProxy, id:"upgradeWithCashBtn", text:Language.getText("@upgrade_building_with_cash_button_label"), onClick: onClickToUpradeWithCashHandler },
			{ skin:"requirementList", classRef: RepeaterProxy, renderProxyRef: BuildingUpgradeRequirementItemRender, id:"requirementList" },
			{ skin:"costList", classRef: RepeaterProxy, renderProxyRef: ResourceRequirementItemRender, id:"costList" }
		].concat(super.proxyMapping);
	}	
	//==========================================================================
	//  Protected methods
	//==========================================================================
	protected function init():void
	{
		var vo:BuildingItemValue = data.buildingItemValue;
		
		icon.load(vo.template.getIconPathByKey("icon"));
		
		skin.nameLabel.text = vo.template.name;
		skin.levelLabel.text = "Lv " + vo.level;
		
		skin.currentLevelLabel.text = "Lv " + vo.level;
		skin.nextLevelLabel.text = "Lv " + (vo.level + 1);
		
		var reqObj:Object = RequirementManager.checkUpgradeRequirement(data.buildingItemValue.nextLevelRequirement);
		isReachRequirement = reqObj.isReached;
		
		requirementList.data = reqObj.cons;
		
		costList.data = vo.nextLevelCost.costValueList;
		
		if (!isReachRequirement) 
		{
			upgradeBtn.enabled = upgradeWithCashBtn.visible = upgradeWithCashBtn.enabled = false;
		}
		else
		{
			if (CostManager.hasEnoughToCost(vo.nextLevelCost, false))
			{
				upgradeBtn.visible = upgradeBtn.enabled = true;
				upgradeWithCashBtn.visible = upgradeWithCashBtn.enabled = false;
			}
			else
			{
				upgradeBtn.visible = upgradeBtn.enabled = false;
				upgradeWithCashBtn.visible = upgradeWithCashBtn.enabled = true;
			}
		}
	}
	//==========================================================================
	//  Event handlers
	//==========================================================================
	protected function onClickToUpradeHandler():void
	{
		if (data.buildingItemValue.setUpgradeCondition())
        {
            GameModel.instance.mapModel.upgradeBuilding(data.buildingItemValue, 
                                                                                                 ItemValue.convertNumbericItemsToServerObject(CostManager.cost(data.buildingItemValue.nextLevelCost)));
        }
		close();
	}
	
	protected function onClickToUpradeWithCashHandler():void
	{
		
	}
	
}
}





